package com.estore.view;

import com.estore.model.Product;
import com.estore.service.ProductService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class ConsoleShop {

    private final ProductService productService = new ProductService();
    // 购物车：key是商品ID，value是购买数量
    private final Map<String, Integer> shoppingCart = new HashMap<>();
    private final Scanner scanner = new Scanner(System.in);

    public void start() {
        while (true) {
            showMenu();
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消费掉换行符

            switch (choice) {
                case 1:
                    browseProducts();
                    break;
                case 2:
                    addToCart();
                    break;
                case 3:
                    viewCart();
                    break;
                case 4:
                    System.out.println("感谢使用，再见！");
                    return;
                default:
                    System.out.println("无效的选项，请重新输入！");
            }
        }
    }

    private void showMenu() {
        System.out.println("\n 欢迎来到骑马商城！！！---- ");
        System.out.println("1. 浏览所有商品");
        getPrintln();
        System.out.println("3. 查看购物车");
        System.out.println("4. 退出");
        System.out.print("请输入您的选择: ");
        //System.out.println("test");
    }

    private static void getPrintln() {
        System.out.println("2. 添加商品到购物车");
    }

    private void browseProducts() {
        System.out.println("\n--- 商品列表 ---");
        System.out.println("请选择排序方式：");
        System.out.println("1. 默认排序");
        System.out.println("2. 按价格升序");
        System.out.println("3. 按价格降序");
        System.out.println("4. 按库存升序");
        System.out.println("5. 按库存降序");
        System.out.print("请输入您的选择: ");
        int sortChoice = scanner.nextInt();
        scanner.nextLine(); // 消费换行符
        
        List<Product> products;
        
        switch (sortChoice) {
            case 1:
                products = productService.getAllProducts();
                break;
            case 2:
                products = productService.getProductsSortedByPrice(true);
                break;
            case 3:
                products = productService.getProductsSortedByPrice(false);
                break;
            case 4:
                products = productService.getProductsSortedByStock(true);
                break;
            case 5:
                products = productService.getProductsSortedByStock(false);
                break;
            default:
                System.out.println("无效的排序选项，将使用默认排序。");
                products = productService.getAllProducts();
        }
        
        if (products.isEmpty()) {
            System.out.println("商店里还没有商品。");
        } else {
            products.forEach(System.out::println);
        }
    }

    private void addToCart() {
        System.out.print("请输入要添加的商品ID: ");
        String productId = scanner.nextLine();
        Product product = productService.findProductById(productId);

        if (product == null) {
            System.out.println("错误：找不到该商品！");
            return;
        }
        if (product.getStock() <= 0) {
            System.out.println("抱歉，该商品已售罄。");
            return;
        }

        System.out.print("请输入购买数量: ");
        int quantity = scanner.nextInt();
        scanner.nextLine(); // 消费换行符

        if (quantity <= 0) {
            System.out.println("错误：购买数量必须为正数！");
            return;
        }
        if (quantity > product.getStock()) {
            System.out.println("错误：库存不足！当前库存为 " + product.getStock());
            return;
        }

        // 将商品加入购物车，如果已存在则累加数量
        shoppingCart.put(productId, shoppingCart.getOrDefault(productId, 0) + quantity);
        System.out.println("成功将 '" + product.getName() + "' 添加到购物车！");
    }

    private void viewCart() {
        System.out.println("\n--- 我的购物车 ---");
        if (shoppingCart.isEmpty()) {
            System.out.println("购物车是空的，快去逛逛吧！");
            return;
        }

        double total = 0.0;
        for (Map.Entry<String, Integer> entry : shoppingCart.entrySet()) {
            String productId = entry.getKey();
            Integer quantity = entry.getValue();
            Product product = productService.findProductById(productId);
            if (product != null) {
                double subtotal = product.getPrice() * quantity;
                System.out.printf("商品: %-20s | 单价: ¥%-8.2f | 数量: %-3d | 小计: ¥%.2f\n",
                        product.getName(), product.getPrice(), quantity, subtotal);
                total += subtotal;
            }
        }
        System.out.println("------------------------------------");
        System.out.printf("总计: ¥%.2f\n", total);
    }

    public static void main(String[] args) {
        new ConsoleShop().start();
    }
}